from django.db import models


# Create your models here.
from django.db.models import F


class BookInfo(models.Model):
    name = models.CharField(max_length=20, verbose_name="名称", unique=True)
    pubDate = models.DateField(verbose_name="发布日期", null=True)
    redCount = models.IntegerField(default=0, verbose_name="阅读量")
    commentCount = models.IntegerField(default=0, verbose_name="评论数")
    isDelete = models.BooleanField(default=False, verbose_name="是否删除")

    class Meta:
        db_table = 'bookinfo'  # 指明数据库表名
        verbose_name = '图书'  # 在admin站点中显示的名称

    def __str__(self):
        """定义每个数据对象的显示信息"""
        return self.name


# insert into bookinfo(name, pubDate, redCount,commentCount, isDelete) values
# ('射雕英雄传', '1980-5-1', 12, 34, 0),
# ('天龙八部', '1986-7-24', 36, 40, 0),
# ('笑傲江湖', '1995-12-24', 20, 80, 0),
# ('雪山飞狐', '1987-11-11', 58, 24, 0);

# 准备人物列表信息的模型类
class PeopleInfo(models.Model):
    GENDER_CHOICES = (
        (0, 'male'),
        (1, 'female')
    )
    name = models.CharField(max_length=20, verbose_name='名称')
    gender = models.SmallIntegerField(choices=GENDER_CHOICES, default=0, verbose_name='性别')
    description = models.CharField(max_length=200, null=True, verbose_name='描述信息')
    book = models.ForeignKey(BookInfo, on_delete=models.CASCADE, verbose_name='图书')  # 外键
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'peopleinfo'
        verbose_name = '人物信息'

    def __str__(self):
        return self.name

    # # insert into peopleinfo(name, gender, book_id, description, is_delete)  values
    # ('郭靖', 1, 1, '降龙十八掌', 0),
    # ('黄蓉', 0, 1, '打狗棍法', 0),
    # ('黄药师', 1, 1, '弹指神通', 0),
    # ('欧阳锋', 1, 1, '蛤蟆功', 0),
    # ('梅超风', 0, 1, '九阴白骨爪', 0),
    # ('乔峰', 1, 2, '降龙十八掌', 0),
    # ('段誉', 1, 2, '六脉神剑', 0),
    # ('虚竹', 1, 2, '天山六阳掌', 0),
    # ('王语嫣', 0, 2, '神仙姐姐', 0),
    # ('令狐冲', 1, 3, '独孤九剑', 0),
    # ('任盈盈', 0, 3, '弹琴', 0),
    # ('岳不群', 1, 3, '华山剑法', 0),
    # ('东方不败', 0, 3, '葵花宝典', 0),
    # ('胡斐', 1, 4, '胡家刀法', 0),
    # ('苗若兰', 0, 4, '黄衣', 0),
    # ('程灵素', 0, 4, '医术', 0),
    # ('袁紫衣', 0, 4, '六合拳', 0);


'''
    #################增加##############
    #方式1
   from book.models import BookInfo,PeopleInfo
   book = BookInfo(
        name='python入门',
       pub_date='2010-1-1'
  )
   book.save()
   #方式2
   PeopleInfo.objects.create(
       name='itheima',
        book=book
   )
    #################修改##############
    #方式1
    person = PeopleInfo.objects.get(name='itheima')
    person.name = 'itcast'
    person.save()
    #方式2
    PeopleInfo.objects.filter(name='itcast').update(name='传智播客')
    #################删除##############
    #方式1
    person = PeopleInfo.objects.get(name='传智播客')
    person.delete()
    #方式2
    BookInfo.objects.filter(name='python入门').delete()
'''
BookInfo.objects.filter(redCount__gt=F('commentCount'))
